#include<iostream>
#include<cassert>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<signal.h>
using std::cout;
using std::endl;
using std::cerr;
void Childprofree(int signo)
{
    assert(signo==SIGCHLD);
   while(true)
   {
        pid_t id=waitpid(-1,nullptr,WNOHANG);
        if(id>0)
        {
            cout<<"Waiting success!pid::"<<getpid()<<"child process id::"<<id<<endl;
        }
        else{
            cout<<"Wating end!"<<endl;
            break;
        }
   }
    
}

int main()
{
    signal(SIGCHLD,Childprofree);
    for(int i=0;i<10;i++)
    {
        pid_t id=fork();
        if(id<0)
        {
            cout<<"Perror fork!"<<endl;
            exit(0);
        }
        else if(id==0)
        {
            int cnt = 0;
            if(i < 6)
                cnt = 5;        
            else
                cnt = 30;     
            while(cnt)
            {
                cout<<"I am child process!my pid::"<<getpid()<<"cnt::"<<cnt--<<endl;
                sleep(2);
            }
            cout<<"Child process being Z!"<<endl;
            exit(0);
        }
    }
  
    while(true)
    {
        cout<<"I am parent process!my pid::"<<getpid()<<endl;
        sleep(2);
    }
    return 0;
}
